import { WorkflowParams, Workflow } from '../interfaces/workflow.interface';
import { WorkflowValidationError } from '../interfaces/errors.interface';

export class WorkflowValidator {
    static validateParams(params: WorkflowParams) {
        if (!params.prompt) {
            throw new WorkflowValidationError('Prompt is required');
        }

        if (params.steps && (params.steps < 1 || params.steps > 150)) {
            throw new WorkflowValidationError('Steps must be between 1 and 150');
        }

        if (params.cfg_scale && (params.cfg_scale < 1 || params.cfg_scale > 30)) {
            throw new WorkflowValidationError('CFG scale must be between 1 and 30');
        }

        if (params.width && (params.width < 64 || params.width > 2048)) {
            throw new WorkflowValidationError('Width must be between 64 and 2048');
        }

        if (params.height && (params.height < 64 || params.height > 2048)) {
            throw new WorkflowValidationError('Height must be between 64 and 2048');
        }
    }

    static validateWorkflow(workflow: Workflow) {
        if (!workflow.nodes || Object.keys(workflow.nodes).length === 0) {
            throw new WorkflowValidationError('Workflow must contain nodes');
        }

        // 验证必要节点是否存在
        const nodeTypes = new Set(
            Object.values(workflow.nodes).map(node => node.type)
        );

        const requiredNodes = ['CLIPTextEncode', 'KSampler', 'VAEDecode'];
        for (const required of requiredNodes) {
            if (!nodeTypes.has(required)) {
                throw new WorkflowValidationError(`Missing required node type: ${required}`);
            }
        }
    }
} 